#include "VideoOcrCommon.hpp"
//查找image轮廓
//src=源图像
//ret_rects=返回找到轮廓矩形数组
//ocr_threshold=二值化处理系数[0-1]区间取灰度0-255
cv::Mat findContoursImage(cv::Mat src,std::vector<cv::Rect> *ret_rects,double ocr_threshold=0.8,bool type=true){
	cv::Mat gray, binary,dst;
	std::vector<cv::Rect> rect_arr;
    //二值化处理
    cv::cvtColor(src, gray, cv::COLOR_BGR2GRAY);
	if (ocr_threshold>1) { ocr_threshold=1.0; }
	if (type) {
		cv::threshold(gray, binary, int(255*ocr_threshold), 255, cv::THRESH_BINARY);
    }else{
		cv::threshold(gray, binary, 0 ,int(255*ocr_threshold), cv::THRESH_BINARY);
	}
    // 形态学处理(可选)
    cv::Mat kernel = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(3,3));
    cv::morphologyEx(binary, binary, cv::MORPH_CLOSE, kernel);
	//cv::imwrite(std::string("/home/knife/test_binary.jpg"),binary);
    // 查找轮廓
    std::vector<std::vector<cv::Point>> contours;
    std::vector<cv::Vec4i> hierarchy;
    cv::findContours(binary, contours, hierarchy, cv::RETR_TREE, cv::CHAIN_APPROX_SIMPLE);
    
    // 3. 绘制轮廓和矩形标记
    dst = src.clone();
    for(size_t i = 0; i < contours.size(); i++) {
		// 绘制轮廓(绿色)
		cv::drawContours(dst, contours, i, cv::Scalar(0, 255, 0), 1);
		
		//计算边界矩形并绘制(红色)
		cv::Rect rect = cv::boundingRect(contours[i]);
		//跳过全屏矩形
		//if ((rect.height*rect.width)==(src.cols*src.rows)){ continue; }
		//std::cout<<"contour="<<i<<":x="<<rect.x<<",y="<<rect.y<<",height="<<rect.height<<",width="<<rect.width<<std::endl;
		rect_arr.push_back(rect);
		// 可选: 显示矩形坐标信息
	}
	ret_rects->assign(rect_arr.begin(),rect_arr.end());
	return dst;
}
//过滤矩形点，去除无关矩形，区域化相关性矩形
std::vector<cv::Rect> filterRects(std::vector<cv::Rect> rects,int pading){
	std::vector<cv::Rect> ret_rects;
	// 按y坐标升序排序
	std::sort(rects.begin(), rects.end(), [](const cv::Rect& a, const cv::Rect& b) {
		return a.y < b.y;
	});
	//初始化最大最小值
	int min_x=0,min_y=0,max_x=0,max_y=0,max_width=0,max_height=0;
	//遍历所有矩形
    for(size_t i = 0; i < rects.size(); i++) {
		//消除小于50*50的矩形
		if (rects[i].width*rects[i].height < 250) { continue; }
		std::cout<<"Rect_"<<i<<":x="<<rects[i].x<<",y="<<rects[i].y<<",height="<<rects[i].height<<",width"<<rects[i].width<<std::endl;

		if (min_x==0){
			min_x=rects[i].x;
			min_y=rects[i].y;
			max_x=rects[i].x;
			max_y=rects[i].y;
			max_height=rects[i].height;
			max_width=rects[i].width;
		}else{
			//按y坐标小于50个坐标点做相关归化
			if (rects[i].y<=(min_y+50)){
				min_y=(rects[i].y)<min_y?rects[i].y:min_y;
				max_y=(rects[i].y)>max_y?rects[i].y:max_y;
				min_x=(rects[i].x)<min_x?rects[i].x:min_x;
				max_x=(rects[i].x)>max_x?rects[i].x:max_x;
				max_height=(rects[i].height)>max_height?rects[i].height:max_height;
				max_width=(rects[i].width)>max_width?rects[i].width:max_width;
			}else{
				ret_rects.push_back(cv::Rect(min_x-pading,min_y-pading,max_x-min_x+max_width+(2*pading),max_height+(2*pading)));
				min_x=rects[i].x;
				min_y=rects[i].y;
				max_x=rects[i].x;
				max_y=rects[i].y;
				max_height=rects[i].height;
				max_width=rects[i].width;
				continue;
			}

		}
	}
	if (min_x>0) {
		ret_rects.push_back(cv::Rect(min_x-pading,min_y-pading,max_x-min_x+max_width+(2*pading),max_height+(2*pading)));
	}
	return ret_rects;
}
//根据轮廓矩形在原图画矩形框
cv::Mat drawRect(cv::Mat dst,std::vector<cv::Rect> rects){
	cv::Mat ret_dst;
    for(size_t i = 0; i < rects.size(); i++) {
		cv::rectangle(dst, rects[i], cv::Scalar(0, 0, 255), 1);
	}
	ret_dst=dst.clone();
	return ret_dst;
}